package EmployeeAndHR;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeMap;

public class HR extends Employee {

	private List<Employee> list = new ArrayList<>(20);
	private char empName='1';
	private int empID=201601;
	private Random random = new Random();
	
	public HR() {
		super();
		name = "HR";
		ID = "201600";
		salary = 3000;

		for (int i = 0; i < 8; i++) {
			enrollEmployee();
		}
	}
	
	
	public List<Employee> getTop4SalaryEmployees() {
		List<Employee> newlist = new ArrayList<>(list);
		List<Employee> result = new ArrayList<>(4);
		Employee e;
		for (int i = 0; i < 4; i++) {
			e = getTopSalaryEmployee(newlist);
			result.add(e);
			newlist.remove(e);
		}
		return result;
	}

	public int getLenth() {
		return this.list.size();
	}

	public List<Employee> getEmployee() {
		return this.list;
	}

	public Employee getTopSalaryEmployee(List<Employee> list) {

		int topSalary = 0;
		int topEmp = 0;
		int current = 0;
		for (int i = 0; i < list.size(); i++) {
			current = list.get(i).getSalary();
			if (current > topSalary) {
				topSalary = current;
				topEmp = i;
			}
		}
		return list.get(topEmp);

	}

	public List<Employee> getList() {
		return list;
	}

	public void setList(List<Employee> list) {
		this.list = list;
	}

	public void enrollEmployee(){
			list.add(new Employee("员工"+String.valueOf(empName++),String.valueOf(empID++),random.nextInt(10)*100+1000));
		
	}
	
	
	public void resignEmployee(String ID){
		for(int i=0;i<list.size();i++){
			if(list.get(i).getID().equals(ID)){
				list.remove(i);
				break;
			}
		}
		
	}
	
	public void print(){
		for(int i=0;i<getLenth();i++){
			System.out.println(list.get(i).toString());
		}
	}
	
	public void salaryDistribution() {
		TreeMap<Integer, Integer> treeMap = new TreeMap<>();
		for (int i = 0; i < list.size(); i++) {
			treeMap.put(i, list.get(i).getSalary());
		}
		int count = 1;
		int salary;
		int current;
		int key1 = 0;
		if (treeMap.isEmpty())
			return;
		int key2;
		while (key1 < treeMap.size()) {
			salary = treeMap.get(key1);
			if (salary == 0) {
				key1++;
				continue;
			}
			key2 = key1 + 1;
			while (key2 < treeMap.size()) {
				current = treeMap.get(key2);
				if (current == 0) {
					key2++;
					continue;
				}
				if (salary == current) {
					treeMap.put(key2, 0);
					count++;
				}
				key2++;
			}
			System.out.println("工资为" + salary + "的员工有" + count + "人");
			count = 1;
			key1++;
		}

	}

}